/*
 * MIT License
 *
 * Copyright (c) 2019 Mr.css
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package cn.seaboot.commons.file;

import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;

/**
 * @author Mr.css
 * @version 2022-02-10 10:49
 */
public class WriterUtils {
    private WriterUtils() {
    }

    // write byte[]
    //-----------------------------------------------------------------------

    /**
     * @param data   需要写入的数据
     * @param output 输出流 <code>OutputStream</code>
     * @throws NullPointerException 输出流为空
     * @throws IOException          发生I/O异常
     */
    public static void write(OutputStream output, byte[] data) throws IOException {
        if (data != null) {
            output.write(data);
        }
    }

    /**
     * @param data   需要写入的数据
     * @param output the <code>Writer</code> to write to
     * @throws NullPointerException 输出流为空
     * @throws IOException          发生I/O异常
     */
    public static void write(Writer output, byte[] data) throws IOException {
        if (data != null) {
            output.write(new String(data));
        }
    }

    /**
     * @param data     需要写入的数据
     * @param output   the <code>Writer</code> to write to
     * @param encoding the encoding to use, null means platform default
     * @throws NullPointerException 输出流为空
     * @throws IOException          发生I/O异常
     */
    public static void write(Writer output, byte[] data, String encoding)
            throws IOException {
        if (data != null) {
            if (encoding == null) {
                write(output, data);
            } else {
                output.write(new String(data, encoding));
            }
        }
    }

    // write char[]
    //-----------------------------------------------------------------------

    /**
     * @param data   需要写入的数据
     * @param output the <code>Writer</code> to write to
     * @throws NullPointerException 输出流为空
     * @throws IOException          发生I/O异常
     */
    public static void write(Writer output, char[] data) throws IOException {
        if (data != null) {
            output.write(data);
        }
    }

    /**
     * @param data   需要写入的数据
     * @param output 输出流 <code>OutputStream</code>
     * @throws NullPointerException 输出流为空
     * @throws IOException          发生I/O异常
     */
    public static void write(OutputStream output, char[] data)
            throws IOException {
        if (data != null) {
            output.write(new String(data).getBytes());
        }
    }

    /**
     * @param data     需要写入的数据
     * @param output   输出流 <code>OutputStream</code>
     * @param encoding the encoding to use, null means platform default
     * @throws NullPointerException 输出流为空
     * @throws IOException          发生I/O异常
     */
    public static void write(OutputStream output, char[] data, String encoding)
            throws IOException {
        if (data != null) {
            if (encoding == null) {
                write(output, data);
            } else {
                output.write(new String(data).getBytes(encoding));
            }
        }
    }

    // write String
    //-----------------------------------------------------------------------

    /**
     * @param data   需要写入的数据
     * @param output the <code>Writer</code> to write to
     * @throws NullPointerException 输出流为空
     * @throws IOException          发生I/O异常
     */
    public static void write(Writer output, String data) throws IOException {
        if (data != null) {
            output.write(data);
        }
    }

    /**
     * @param data   需要写入的数据
     * @param output 输出流 <code>OutputStream</code>
     * @throws NullPointerException 输出流为空
     * @throws IOException          发生I/O异常
     */
    public static void write(OutputStream output, String data)
            throws IOException {
        if (data != null) {
            output.write(data.getBytes());
        }
    }

    /**
     * @param data     需要写入的数据
     * @param output   输出流 <code>OutputStream</code>
     * @param encoding the encoding to use, null means platform default
     * @throws NullPointerException 输出流为空
     * @throws IOException          发生I/O异常
     */
    public static void write(OutputStream output, String data, String encoding)
            throws IOException {
        if (data != null) {
            if (encoding == null) {
                write(output, data);
            } else {
                output.write(data.getBytes(encoding));
            }
        }
    }
}
